﻿using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace R.FarmSmartSleep.RBAC.API.Apliction.Command
{
    public class PhoneUpdateHandler:IRequestHandler<PhoneUpdateCommon,Result>
    {
        private readonly IBaseRepository<Employee> employeeRep;

        public PhoneUpdateHandler(IBaseRepository<Employee> employeeRep)
        {
            this.employeeRep = employeeRep;
        }
        /// <summary>
        /// 更新员工手机号
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PhoneUpdateCommon request, CancellationToken cancellationToken)
        {
            var result= new Result() { Code=(int)CodeEnum.update};
            var list =  employeeRep.GetAll().Where(c =>  c.EmployeeId == request.employeeId).FirstOrDefault();
            list.PhoneNumber= request.Phone;
            var n = await employeeRep.UpdateAsync(list);
            if(n>0)
            {
                result.Code = (int)CodeEnum.success;
            }
            return result; 
        
        }
    }
}
